package com.eoner.baselib.utils.activity;

import android.app.Activity;
import android.os.Process;
import android.util.Log;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Activity采集器.
 * @author FZ
 */ 
public class UtilActivity {
	
	private UtilActivity(){
		
	}
	
	/**
	 * 
	 */
	private static List<Activity> activities = new ArrayList<Activity>();

	public static List<Activity> getActivities() {
		return activities;
	}

	/**
	 * @param activity
	 */
	public static void addActivity(Activity activity){
		if(!activities.contains(activity)){
			activities.add(activity);
		}
	}

	/**
	 * 获取当前Activity（堆栈中最后一个压入的）
	 */
	public static Activity currentActivity() {
		Activity activity = null;
		if(activities != null && activities.size()>0){
			activity = activities.get(activities.size()-1);
		}
		return activity;
	}


	/**
	 * @param
	 */
	public static Activity getBeforeActivity(){
		Activity activity = null;
		if(activities != null && activities.size() > 1){
			activity = activities.get(activities.size()-2);
		}
		return activity;
	}

	/**
	 * @param activity
	 */
	public static void removeActivity(Activity activity){
		activities.remove(activity);
	}
	
	/**
	 * 杀死进程
	 */
	public static void finishAll(){
		for(Activity activity : activities){
			activity.finish();
		}
		Process.killProcess(Process.myPid());
	}
	
	/**
	 * 除第一个界面外关闭所有界面
	 */
	public static void onlyFirst(){
		for (int i = 0; i < activities.size(); i++) {
			if(i != 0){
				activities.get(i).finish();
			}
		}
	}
	
	/**
	 * 关闭从栈顶开始的num个界面
	 */
	public static void finishNum(int num){
		Collections.reverse(activities);
		for (int i = 0; i < num; i++) {
			activities.get(i).finish();
		}
		Collections.reverse(activities);
	}

	/**
	 * 关闭从num界面开始的num个界面
	 */
	public static void finishStartNum(int start, int num){
		Collections.reverse(activities);
		for (int i = 0; i < (start + num); i++) {
			if(i >= start) {
				activities.get(i).finish();
			}
		}
		Collections.reverse(activities);
	}
	
	/**
	 * 判断进程是否已经被杀死	true:已杀死	false:未杀死
	 */
	public static boolean isProcessKill(){
		if(activities.size() == 0 || activities == null){
			return true;
		}else{
			return false;
		}	
	}

	/**
	 * 获取Activity
	 */
	public static Activity getActivity(int num){
		return activities.get(num);
	}

	/**
	 * 获取Activitys的长度
	 */
	public static int getActivitySize(){
		if(activities != null) {
			return activities.size();
		} else {
			return 0;
		}
	}
	public static boolean isCurActivity(Activity activity){

		if (activities.contains(activity)){
			return true;
		}
		return false;
	}

	public static boolean isCurActivity(String activityName){
		for (Activity act: activities) {
			Log.e("======isCurActivity", act.getLocalClassName()+"==="+act.getLocalClassName().substring(act.getLocalClassName().lastIndexOf(".")+1));
			if (act.getLocalClassName().substring(act.getLocalClassName().lastIndexOf(".")+1).equals(activityName)){
				return true;
			}
		}
		return false;
	}
}
